<?php

namespace App\Jobs\Order;

use App\Lib\WeApp\WeApp;
use App\Models\WxOrder;
use App\Wen\Utils\OrderUtils;
use App\Wen\Utils\Settings;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class OrderDeliverInfoManage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $action = '';
    protected $order_id = 0;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($action, $order_id)
    {
        //
        $this->action = $action;
        $this->order_id = $order_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $weapp = new WeApp('mini');
        $deliver_manage = $weapp->getDeliverInfoManage();
        if(Settings::get('need_mini_deliver_info_manage', 0) != 1){
            return;
        }

        if($this->order_id > 0 && !_empty_($this->action)){
            $order = WxOrder::find($this->order_id);
            if(_empty_($order)){
                return;
            }
            // 特定动作
            if($this->action == 'upload'){
                if($order->order_state == 3 && !_empty_($order->order_serial_number) && $order->order_serial_platform === 0 && $order->order_serial_platform_type == 'mini'){
                    $deliver_manage->virtualDeliverOrder($order);
                }
            }else if($this->action == 'query'){
                if($order->order_state == 5 && !_empty_($order->order_serial_number) && $order->order_serial_platform === 0 && $order->order_serial_platform_type == 'mini'){
                    $infomange = $weapp->getDeliverInfoManage();
                    $wechat_order = $infomange->getDeliverOrder($order->order_serial_number);
                    if($wechat_order){
                        if(in_array(_array_key($wechat_order, 'order_state', 0), [3,4])){
                            DB::beginTransaction();
                            try {
                                WxOrder::where('id', $order->id)->update([
                                    'order_state' => 1
                                ]);
                                OrderUtils::order_obtains($order, 0);
                                DB::commit();
                            } catch (\Exception $e) {
                                DB::rollBack();
                            }
                        }
                    }
                }
            }
        }else{
            if($this->action == 'upload') {
                // 上传
                WxOrder::whereNotNull('order_serial_number')->where('order_serial_number', '<>', '')->where('order_serial_platform', 0)
                    ->where('order_serial_platform_type', 'mini')->where('order_state', 3)->limit(30)->get()->map(function ($order) use (&$deliver_manage) {
                        $deliver_manage->virtualDeliverOrder($order);
                    });
            }else if($this->action == 'query'){
                WxOrder::whereNotNull('order_serial_number')->where('order_serial_number', '<>', '')->where('order_serial_platform', 0)
                    ->where('order_serial_platform_type', 'mini')->where('order_state', 5)->limit(30)->get()->map(function ($order) use (&$deliver_manage) {
                        $wechat_order = $deliver_manage->getDeliverOrder($order->order_serial_number);
                        if($wechat_order){
                            if(in_array(_array_key($wechat_order, 'order_state', 0), [3,4])){
                                DB::beginTransaction();
                                try {
                                    WxOrder::where('id', $order->id)->update([
                                        'order_state' => 1
                                    ]);
                                    OrderUtils::order_obtains($order, 0);
                                    DB::commit();
                                } catch (\Exception $e) {
                                    DB::rollBack();
                                }
                            }
                        }
                });
            }else{
                OrderDeliverInfoManage::dispatch('upload', 0);
                OrderDeliverInfoManage::dispatch('query', 0);
            }
        }
    }
}
